//
//  CFBTGetData.m
//  半糖框架
//
//  Created by afro on 15/12/15.
//  Copyright © 2015年 afro. All rights reserved.
//

#import "CFBTGetDataTool.h"
#import "CFBTBannerModel.h"
#import "CFBTTopicModel.h"
#import "CFBTCategoryElementModel.h"

@implementation CFBTGetDataTool

SingleM(CFBTGetDataTool)

- (CFBTAllModel *)allModel {
    if (_allModel == nil) {
        _allModel = [[CFBTAllModel alloc] init];
    }
    return _allModel;
}
// 由外界确定请求路径,请求类型
- (void)dataRequestWithURL:(NSString *)url requestType:(BOOL)GETorPOST success:(successBlock)successBlock fail:(failBlock)failBlock
{
    
    // 1. 创建会话管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    if (GETorPOST) {
        // 2. 如果为YES则发送POST请求
        [manager POST:url
          parameters:[self getRequestParamsDict]
             success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
                 
                 // 3. 拿到服务器响应数据
                 NSDictionary *dict = responseObject[@"data"];
                 
                 if(dict != nil){
                     
                     successBlock(self.allModel);
                     
                 }else{
                     
                     failBlock(responseObject);
                     
                 }
                 
             } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                 
                 NSLog(@"请求失败");
                 
             }];
    } else {
    // 4. 如果为NO则发送GET请求,请求参数调用自定义的方法
    [manager GET:url
      parameters:[self getRequestParamsDict]
         success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
             
             //NSLog(@"%@",responseObject);
             
             // 5. 拿到服务器响应数据
             NSDictionary *dict = responseObject[@"data"];
             
             // 6. 数组转模型,告诉系统,CFBTAllModel中的banner数组存放的是CFBTBannerModel模型
             [CFBTAllModel setupObjectClassInArray:^NSDictionary *{
                 return @{@"banner" : [CFBTBannerModel class]};
             }];
             
             [CFBTAllModel setupObjectClassInArray:^NSDictionary *{
                 return @{@"topic" : [CFBTTopicModel class]};
             }];
             
             [CFBTAllModel setupObjectClassInArray:^NSDictionary *{
                 return @{@"category_element" : [CFBTCategoryElementModel class]};
             }];
             
             // 6.1 第一层数据转模型
             self.allModel = [CFBTAllModel objectWithKeyValues:dict];
             
             // 6.2 第二层数据转模型
             self.allModel.banner = [CFBTBannerModel objectArrayWithKeyValuesArray:dict[@"banner"]];
             self.allModel.topic = [CFBTTopicModel objectArrayWithKeyValuesArray:dict[@"topic"]];
             self.allModel.category_element = [CFBTCategoryElementModel objectArrayWithKeyValuesArray:dict[@"category_element"]];
             
             if(dict != nil){
                 
                 successBlock(self.allModel);
                 
             }else{
                 
                 failBlock(responseObject);
                 
             }
             
         } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
             
             NSLog(@"请求失败");
             
         }];
    }
}

// 设置请求参数
- (NSDictionary *)getRequestParamsDict {
    /*
     2.1 创建请求参数
     请求参数:
     !!!注意,如果未登录就没有oauth_token字段
     !!!具体参数参考真机,青花瓷截取
     @"app_installtime":@"1449644055.35003"
     @"app_versions":@"5.1"
     @"channel_name":@"appStore"
     @"client_id":@"bt_app_ios"
     @"client_secret":@"9c1e6634ce1c5098e056628cd66a17a5"
     @"os_versions":@"8.4"
     @"page":@"0"
     @"pagesize":@"20"
     @"screensize":@"640"
     @"track_device_info":@"iPhone6,2"
     @"track_deviceid":@"E4E260D3-A97D-4BB8-B10E-8BD59071D235"
     @"v":@"9"
     */
    
    NSDictionary *requestDict = @{
                          @"app_installtime":@"1450229090.9476",
                          @"app_versions":@"5.1.1",
                          @"channel_name":@"appStore",
                          @"client_id":@"bt_app_ios",
                          @"client_secret":@"9c1e6634ce1c5098e056628cd66a17a5",
                          @"os_versions":@"8.4",
                          @"page":@"0",
                          @"pagesize":@"20",
                          @"screensize":@"640",
                          @"track_device_info":@"iPhone6,2",
                          @"track_deviceid":@"495C2CBE-FD23-485D-8B7A-667D4DDC67ED",
                          @"v":@"9"
                    };
    return requestDict;
}
@end
